﻿using System;
using System.Collections.Generic;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;


public partial class PowerWarning_RateChangBEdit : System.Web.UI.Page
{
    private string sEditStatus = "1";
    private ChangeRateBLL bll = new ChangeRateBLL();
    private string sID = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            sID = this.Request.QueryString["ID"].ToString().Trim();
        }
        catch
        { }
        if (!IsPostBack)
        {

            if (sEditStatus == "2")
            {
                ChangeRate model = bll.GetModel(int.Parse(sID));
                if (model != null)
                {
                    txtTelecomRate.Text = model.fTelecomRate.ToString();
                    txtBaseRate.Text = model.fBaseRate.ToString();
                    txtBusinessRate.Text = model.fBusinessRate.ToString();
                    sltYear.Value = model.Years;
                }
            }
        }
    }
    /// <summary>
    /// 计算按钮事件 根据给出的三个下降率 计算预计总耗电量和计算后的三个下降率 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        //用以返回结果的集合
        Dictionary<string, string> dicRlt = new Dictionary<string, string>();
        //三目标导向分析过程
        string sBack = bll.GoalOrientedProcess(txtTelecomRate.Text.Trim(), txtBaseRate.Text.Trim(), txtBusinessRate.Text.Trim(), sltYear.Value, dicRlt);
        if (String.IsNullOrEmpty(sBack))
        {
            //----------------计算过程还未给出--------------------------------//
            labTelecomRatePower.Text = Convert.ToDouble(dicRlt["dPower1"]).ToString("0.00");
            labBaseRatePower.Text = Convert.ToDouble(dicRlt["dPower2"]).ToString("0.00");
            labBusinessRatePower.Text = Convert.ToDouble(dicRlt["dPower3"]).ToString("0.00");
            labMinPowers.Text = Convert.ToDouble(dicRlt["MinPower"]).ToString("0.00");
            //填充页面信息
            //labTelecomRate.Text = Convert.ToDouble(dicRlt["MinPower"]).ToString("0.00");
            labTelecomRate.Text = Convert.ToDouble(dicRlt["TelecomRate"]).ToString("0.00");
            labBaseRate.Text = Convert.ToDouble(dicRlt["BaseRate"]).ToString("0.00");
            labBusinessRate.Text = Convert.ToDouble(dicRlt["BusinessRate"]).ToString("0.00");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + sBack + "');</script>");
            return;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ChangeRate model = new ChangeRate();
        model.fTelecomRate = decimal.Parse(txtTelecomRate.Text.Trim());
        model.fBaseRate = decimal.Parse(txtBaseRate.Text.Trim());
        model.fBusinessRate = decimal.Parse(txtBusinessRate.Text.Trim());
        model.Years = sltYear.Value;
        model.TelecomRate = decimal.Parse(labTelecomRate.Text);
        model.BaseRate = decimal.Parse(labBaseRate.Text);
        model.BusinessRate = decimal.Parse(labBusinessRate.Text);
        model.OperatDate = DateTime.Now;
        model.OperatUser = DateTime.Now;
        model.Type = "2";
        model.Powers = decimal.Parse(labMinPowers.Text);
        int iBack = 0;
        if (sEditStatus == "1")
        {
            iBack = bll.Add(model);
            if (iBack == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                return;
            }
        }
        else
        {
            model.CR_ID = int.Parse(sID);
            iBack = bll.Update(model);
            if (iBack == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                return;
            }
        }
    }
}
